package com.keqing.tomato.fragment;

import static com.keqing.tomato.R.layout.fragment_settings;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;

import com.keqing.tomato.AbountMeActivity;
import com.keqing.tomato.R;
import com.keqing.tomato.RadioListActivity;
import com.keqing.tomato.UseManualActivity;
import com.keqing.tomato.util.PreferenceUtil;


public class FragmentSettings extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    public static final int request_code_time_focus = 0;
    public static final int request_code_time_rest = 1;
    public static final int request_code_sound_focus = 2;
    public static final int request_code_sound_rest = 3;
    private TextView tv_focus_time;
    private TextView tv_rest_time;
    private TextView tv_tomato_finish_ring;
    private TextView tv_tomato_rest_ring;
    private SwitchCompat switch_vibration;
    private SwitchCompat switch_auto_rest;
    private Activity activity;
    private PreferenceUtil.MySetting mySetting;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("com/keqing/tomato", "FragmentSettings - onCreateView()");
        return inflater.inflate(fragment_settings, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e("com/keqing/tomato", "FragmentSettings - onViewCreated()");
        activity = getActivity();

        tv_rest_time = view.findViewById(R.id.tv_rest_time);
        tv_focus_time = view.findViewById(R.id.tv_focus_time);
        View rest_time_container = view.findViewById(R.id.rest_time_container);
        View tomato_finish_ring_container = view.findViewById(R.id.tomato_finish_ring_container);
        View rest_finish_ring_container = view.findViewById(R.id.rest_finish_ring_container);
        tv_tomato_finish_ring = view.findViewById(R.id.tv_tomato_finish_ring);
        tv_tomato_rest_ring = view.findViewById(R.id.tv_tomato_rest_ring);
        TextView use_manual = view.findViewById(R.id.use_manual);
        TextView abount_me = view.findViewById(R.id.abount_me);
        switch_vibration = view.findViewById(R.id.switch_vibration);
        switch_auto_rest = view.findViewById(R.id.switch_auto_rest);
        View focus_time_container = view.findViewById(R.id.focus_time_container);

        focus_time_container.setOnClickListener(this);
        rest_time_container.setOnClickListener(this);
        tomato_finish_ring_container.setOnClickListener(this);
        rest_finish_ring_container.setOnClickListener(this);
        use_manual.setOnClickListener(this);
        abount_me.setOnClickListener(this);
        switch_auto_rest.setOnCheckedChangeListener(this);
        switch_vibration.setOnCheckedChangeListener(this);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e("com/keqing/tomato", "FragmentSettings - onHiddenChanged()");
        if (!hidden) {
            initSettings();
        }
    }

    @SuppressLint("SetTextI18n")
    private void initSettings() {
        mySetting = PreferenceUtil.getMySetting(activity);
        tv_focus_time.setText(mySetting.getFocusTime() + "分钟");
        tv_rest_time.setText(mySetting.getRestTime() + "分钟");
        tv_tomato_finish_ring.setText(mySetting.getFocusSound());
        tv_tomato_rest_ring.setText(mySetting.getRestSound());
        switch_vibration.setChecked(mySetting.isVibration());
        switch_auto_rest.setChecked(mySetting.isAutoRest());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("com/keqing/tomato", "FragmentSettings - onDestroy()");
    }

    //页面跳转
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.focus_time_container:
                Intent intent = new Intent(activity, RadioListActivity.class);
                intent.putExtra("requestCode", request_code_time_focus);
                startActivityForResult(intent, request_code_time_focus);
                break;
            case R.id.rest_time_container:
                intent = new Intent(activity, RadioListActivity.class);
                intent.putExtra("requestCode", request_code_time_rest);
                startActivityForResult(intent, request_code_time_rest);
                break;
            case R.id.tomato_finish_ring_container:
                intent = new Intent(activity, RadioListActivity.class);
                intent.putExtra("requestCode", request_code_sound_focus);
                startActivityForResult(intent, request_code_sound_focus);
                break;
            case R.id.rest_finish_ring_container:
                intent = new Intent(activity, RadioListActivity.class);
                intent.putExtra("requestCode", request_code_sound_rest);
                startActivityForResult(intent, request_code_sound_rest);
                break;
            case R.id.use_manual:
                intent = new Intent(activity, UseManualActivity.class);
                startActivity(intent);
                break;
            case R.id.abount_me:
                intent = new Intent(activity, AbountMeActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        initSettings();
    }

    //switch切换
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.isPressed()) {
            switch (buttonView.getId()) {
                case R.id.switch_auto_rest:
                    mySetting.setAutoRest(isChecked);
                    break;
                case R.id.switch_vibration:
                    mySetting.setVibration(isChecked);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + buttonView.getId());
            }
        }
    }
}